package com.cennavi.minemapdemo.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.cennavi.minemapdemo.R;
import com.cennavi.minemapdemo.util.Config;

public class LauncherActivity extends Activity {

    private SharedPreferences mSharedPreferences;
    private Context mContext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_launcher);
        mContext = this;
        initLocalConfig();

        findViewById(R.id.btn_init).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startMainActivity();
            }
        });

        findViewById(R.id.btn_set).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startSettingActivity();
            }
        });
    }

    private void startMainActivity() {
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
    }

    private void startSettingActivity() {
        Intent intent = new Intent(this, SettingActivity.class);
        startActivity(intent);
    }

    /**
     * @param context
     * @param key
     * @param defaultValues
     * @return
     */
    private String getString(Context context, String key, String defaultValues) {
        if (mSharedPreferences == null) {
            mSharedPreferences = context.getSharedPreferences("config", Context.MODE_PRIVATE);
        }
        return mSharedPreferences.getString(key, defaultValues);
    }

    private void initLocalConfig() {
        String serverHost = getString(mContext, "MineMap_serverHost", "");

        if (!TextUtils.isEmpty(serverHost)) {
            Config.HOST = serverHost;
        }

        String serverScheme = getString(mContext, "MineMap_serverScheme", "");

        if (!TextUtils.isEmpty(serverScheme)) {
            Config.SCHEME = serverScheme;
        }

        String mapID = getString(mContext, "MineMap_mapID", "");

        if (!TextUtils.isEmpty(mapID)) {
            Config.MAP_ID = mapID;
        }

        String mapTok = getString(mContext, "MineMap_mapTok", "");

        if (!TextUtils.isEmpty(mapTok)) {
            Config.MAP_TOK = mapTok;
        }

        String mapNightID = getString(mContext, "MineMap_mapNightID", "");

        if (!TextUtils.isEmpty(mapNightID)) {
            Config.MAP_NIGHT_ID = mapNightID;
        }

        String mapNightTok = getString(mContext, "MineMap_mapNightTok", "");

        if (!TextUtils.isEmpty(mapNightTok)) {
            Config.MAP_NIGHT_TOK = mapNightTok;
        }

        String satelliteUrl = getString(mContext, "MineMap_satelliteUrl", "");

        if (!TextUtils.isEmpty(satelliteUrl)) {
            Config.SATELLITE_URL = satelliteUrl;
        }

        String offlineUrl = getString(mContext, "MineMap_offlineUrl", "");

        if (!TextUtils.isEmpty(offlineUrl)) {
            Config.OFFLINE_URL = offlineUrl;
        }

        String offlineTok = getString(mContext, "MineMap_offlineTok", "");

        if (!TextUtils.isEmpty(offlineTok)) {
            Config.OFFLINE_TOK = offlineTok;
        }

        String apiBaseUrl = getString(mContext, "MineMap_apiBaseUrl", "");

        if (!TextUtils.isEmpty(apiBaseUrl)) {
            Config.API_BASE_URL = apiBaseUrl;
        }

        String apiUrl = getString(mContext, "MineMap_apiUrl", "");

        if (!TextUtils.isEmpty(apiUrl)) {
            Config.API_URL = apiUrl;
        }

        String spriteGlyphsUrl = getString(mContext, "MineMap_spriteGlyphsUrl", "");

        if (!TextUtils.isEmpty(spriteGlyphsUrl)) {
            Config.SPRITE_GLYPHS_URL = spriteGlyphsUrl;
        }
    }
}
